<template>
  <div class="app-container" style="margin-top:10px">
    <el-card>
      <div class="amap-page-container">
        <el-input
          class="inpu"
          placeholder="请输入你要定位的地址"
          v-model="address"
        >
          <i slot="prefix" class="el-input__icon el-icon-search"></i>
        </el-input>
        <el-button type="primary" @click="searchMap()">定位</el-button>
        <el-amap vid="amap" :plugin="plugin" class="amap-demo" :center="center" :zoom="zoom" :events="events">
          <!-- 点击显示标记 -->
          <el-amap-marker v-for="(marker, index) in markers" :key="marker.index" :position="marker.position"
                          :icon="marker.icon" :title="marker.title" :events="marker.events" :visible="marker.visible"
                          :draggable="marker.draggable" :vid="index"
          ></el-amap-marker>
        </el-amap>
      </div>
      <div class="filter-container" style="margin-top:50px">
        <!--      <el-input-->
        <!--        class="filter-item"-->
        <!--        placeholder="输入关键字进行过滤"-->
        <!--        v-model="listQuery.keyword"-->
        <!--        style="width: 30%"-->
        <!--      >-->
        <!--      </el-input>-->
        <!--      <el-input v-model="listQuery.title" placeholder="Title" style="width: 200px;" class="filter-item"-->
        <!--                @keyup.enter.native="handleFilter"-->
        <!--      />-->
        <!--      <el-select v-model="listQuery.importance" placeholder="Imp" clearable style="width: 90px" class="filter-item">-->
        <!--        <el-option v-for="item in importanceOptions" :key="item" :label="item" :value="item" />-->
        <!--      </el-select>-->
        <!--      <el-select v-model="listQuery.type" placeholder="Type" clearable class="filter-item" style="width: 130px">-->
        <!--        <el-option v-for="item in calendarTypeOptions" :key="item.key" :label="item.display_name+'('+item.key+')'" :value="item.key" />-->
        <!--      </el-select>-->
        <!--      <el-select v-model="listQuery.sort" style="width: 140px" class="filter-item" @change="handleFilter">-->
        <!--        <el-option v-for="item in sortOptions" :key="item.key" :label="item.label" :value="item.key" />-->
        <!--      </el-select>-->
        <!--      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="getList"-->
        <!--                 style="margin-left: 10px"-->
        <!--      >-->
        <!--        搜索-->
        <!--      </el-button>-->
        <!--      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit"-->
        <!--                 @click="handleCreate"-->
        <!--      >-->
        <!--        添加管理员-->
        <!--      </el-button>-->
        <!--      <el-button type="success" class="filter-item" @click="tijiao">完成</el-button>-->
        <!--      <el-button v-waves :loading="downloadLoading" class="filter-item" type="primary" icon="el-icon-download"-->
        <!--                 @click="handleDownload"-->
        <!--      >-->
        <!--        导出-->
        <!--      </el-button>-->
        <!--      <el-checkbox v-model="showReviewer" class="filter-item" style="margin-left:15px;" @change="tableKey=tableKey+1">-->
        <!--        审核-->
        <!--      </el-checkbox>-->
      </div>
      <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="120px"
               style="width: 80%; margin-left:10%;"
      >
        <br/>
        <el-form-item label="纬度">
          <el-input v-model="temp.lat"/>
        </el-form-item>
        <el-form-item label="经度">
          <el-input v-model="temp.lng"/>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { detail, create } from '@/api/company'
// import { getMenus } from '@/api/menu'

export default {
  name: 'CompanyList',
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'info',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    let self = this
    return {
      imagecropperShow: false,
      imagecropperKey: 0,
      menus: [],
      roles: [],
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        keyword: ''
      },
      temp: {
        id: undefined,
        lat: '',
        lng: '',
        tel: '',
        phone: '',
        address: '',
        email: ''
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '创建'
      },
      filterText: '',
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      rules: {
        name: [{ required: true, message: '身份不能为空', trigger: 'change' }]
      },
      tishi: '',
      //从这里下去是地图有关的
      address: '',  //获取的位置
      zoom: 13,  // 地图缩放
      center: [122.59996, 26.197646],  // 初始中心
      lng: 0,  //经纬度
      lat: 0,
      loaded: false,
      // 点击显示的标记默认的定位
      markers: [],
      //  自动定位到当前位置
      plugin: [{
        timeout: 1000, //超过10秒后停止定位，默认：无穷大
        panToLocation: true, //定位成功后将定位到的位置作为地图中心点，默认：true
        zoomToAccuracy: true, //定位成功后调整地图视野范围使定位位置及精度范围视野内可见，默认：f
        pName: 'Geolocation',
        events: {
          init(o) {
            // o 是高德地图定位插件实例
            o.getCurrentPosition((status, result) => {
              if (result && result.position) {
                self.address = result.formattedAddress
                self.lng = result.position.lng
                self.lat = result.position.lat
                self.center = [self.lng, self.lat]
                self.markers = [{
                  position: self.center
                }]
                self.loaded = true
                self.$nextTick()
              } else {
                o.getCityInfo((status, result) => {
                  if (result && result.center) {
                    // self.address = result.formattedAddress;
                    self.temp.lng = self.lng = result.center[0]
                    self.temp.lat = self.lat = result.center[1]
                    self.center = result.center
                    self.markers = [{
                      position: self.center
                    }]
                    self.loaded = true
                    self.$nextTick()
                  }
                })
              }
            })
          }
        }
      }],
      // 点击地图获取当前位置并显示标记
      events: {
        click(e) {
          self.chaadd(e.lnglat)
        }
      }
    }
  },
  watch: {
    filterText(val) {
      this.$refs.tree.filter(val)
    }
  },
  created() {
    this.getList()
  },
  methods: {
    searchMap() {
      let that = this
      let address = this.address
      this.temp.address = this.address
      var geocoder = new AMap.Geocoder({
        city: '' //城市设为北京，默认：“全国”
      })
      geocoder.getLocation(address, function(status, result) {
        if (status === 'complete' && result.geocodes.length) {
          var lnglat = result.geocodes[0].location
          that.center = [lnglat.lng, lnglat.lat]
          that.lng = lnglat.lng
          that.lat = lnglat.lat
          that.markers = [{
            position: that.center
          }]
        } else {
          console.log('根据地址查询位置失败')
        }
      })
    },
    chaadd(e) {
      let self = this
      let { lng, lat } = e
      self.temp.lng = self.lng = lng
      self.temp.lat = self.lat = lat
      self.center = [self.lng, self.lat]
      self.loaded = true
      self.markers = [{
        position: self.center
      }]
      var geocoder = new AMap.Geocoder({
        radius: 1000 //范围，默认：500
      })
      var marker = new AMap.Marker()

      function regeoCode() {
        var lnglat = [lng, lat]
        geocoder.getAddress(lnglat, function(status, result) {
          if (status === 'complete' && result.regeocode) {
            self.temp.address = self.address = result.regeocode.formattedAddress
          } else {
            console.log('根据经纬度查询地址失败')
          }
        })
      }

      regeoCode()
    },
    cropSuccess(resData) {
      this.imagecropperShow = false
      this.imagecropperKey = this.imagecropperKey + 1
      this.temp.avatar = resData.url
    },
    close() {
      this.imagecropperShow = false
    },
    filterNode(value, data) {
      if (!value) return true
      return data.label.indexOf(value) !== -1
    },
    getCheckedNodes() {
      console.log(this.$refs.tree.getCheckedNodes())
    },
    setCheckedNodes(row) {
      this.$refs.tree.setCheckedNodes(row)
    },

    getList() {
      this.listLoading = true
      detail().then(response => {
        this.temp = response.data
        this.lng = response.data.lng
        this.address = response.data.address
        this.lat = response.data.lat
      })
    },
    resetForm() {
      this.dialogFormVisible = false
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          create(this.temp).then(response => {
            // this.list.unshift(this.temp)
            this.dialogFormVisible = false
            if (response.code == 200) {
              this.$notify.success({
                title: '成功',
                message: response.msg,
                type: 'success',
                duration: 2000
              })
              this.resetTemp()
              this.getList()
            } else {
              this.$notify.error({
                title: '失败',
                message: response.msg,
                duration: 2000
              })
            }
          })
        }
      })
    },
    resetTemp() {
      this.temp = {
        id: undefined,
        lat: '',
        lng: '',
        tel: '',
        phone: '',
        address: '',
        email: ''
      }
    }
  }
}
</script>

<style scoped>
.edit-input {
  padding-right: 100px;
}

.cancel-btn {
  position: absolute;
  right: 15px;
  top: 10px;
}
</style>
<style scoped>
.avatar {
  width: 200px;
  height: 200px;
  border-radius: 50%;
}
</style>
<style>
.amap-page-container {
  height: 400px;
  padding-top: 0px;
}

.inpu {
  width: calc(100% - 120px);
  margin-right: 20px;
  margin-bottom: 20px;
}

.wan {
  margin-top: 20px;
}
</style>
